package chat;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @Author GongXl
 * @Description 爬虫代码测试
 * @Date 004 12:07
 * @Param
 * @return
**/
public class UrlTest implements Runnable {
    private CyclicBarrier cyclicBarrier;

    private String picUrl;
    public UrlTest(String picUrl, CyclicBarrier cyclicBarrier){
        this.picUrl = picUrl;
        this.cyclicBarrier = cyclicBarrier;
    }
    @Override
    public void run() {
        //下载资源的
        System.out.println("资源RUL:"+picUrl);
        long start = System.currentTimeMillis();
        try {
            downLoadImg(picUrl);//URL资源下载
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName()+"个线程下载耗时："+(end-start));
        if(end-start > maxTime){
            maxTime = end-start;
            System.out.println("#####"+maxTime);
        }
        int await = 0;
        try {
            await = cyclicBarrier.await();
            System.out.println(Thread.currentThread().getName()+"await:"+await);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
    static List<String> pics = new ArrayList<String>();
    private static long maxTime = 0;
    /**
     * @Author GongXl
     * @Description 下载指定网站的图片
     * @Date 004
     * @Param [args]
     * @return void
    **/
    public static void main(String[] args) throws IOException {
        String webUrl = "https://www.tsinghua.edu.cn/";//要下这个网的图片
//        getSrcReg(webUrl);

        try {
            URL url=new URL(webUrl);
            getSrcReg(webUrl);

            //栅栏
            CyclicBarrier barrier = new CyclicBarrier(pics.size(), new Runnable() {
                //栅栏动作，在计数器为0的时候执行
                @Override
                public void run() {
                    System.out.println("资源下载完成,最大耗时时间："+maxTime);
                }
            });
            ExecutorService es = Executors.newCachedThreadPool();
            for (int i = 0; i <  pics.size(); i++) {
                String picUrlTemp = webUrl + pics.get(i);
                es.execute(new UrlTest(picUrlTemp,barrier));
//                new Thread().start();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void downLoadImg(String urlAddress) throws IOException {
//        String urlAddress = "https://www.baidu.com/img/baidu_jgylogo3.gif";
        String fileName = urlAddress.substring(urlAddress.lastIndexOf("/"));
        InputStream is = null;
        OutputStream os = null;
        URL url = null;
        try {
            url = new URL(urlAddress);
            is = url.openStream();
            File filePath = new File("e:/tmp/qinghuaRes/"+fileName);
            if(! new File(filePath.getParent()).exists()){
                new File(filePath.getParent()).mkdirs();
            }
            os = new FileOutputStream(filePath);
            byte[] bys = new byte[1024];
            int len = 0;
            while((len = is.read(bys))!=-1){
                os.write(bys, 0, len);
            }
            System.out.println("下载成功："+filePath.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != os){
                os.close();
            }
            if(null != is){
                is.close();
            }
        }
    }
    /**
     * @Author GongXl
     * @Description 获得src
     * @Date 004 10:53
     * @Param [url]
     * @return void
    **/
    public static void  getSrcReg(String url) throws IOException {
//        String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
        String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF|js|JS|)))('|\")";
//        String searchImgReg2 = "(?x)(src|SRC|background|BACKGROUND)=('|\")((http|https)://([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
        String content = getHtmlCode(url);
        System.out.println(content);

        Pattern pattern = Pattern.compile(searchImgReg);
        //<div><a href="https://www.tsinghua.edu.cn/kyzt/" target="_blank"><img src="js/abc/fdasfda/fdasfdas/adf/asfasf/adsf/safsaf/sa/f/as/fas/f/saf/   zwkyfdsafdasfsafa-dsafadsfd-fdsafas.jpg" style="width: 100%;"></a></div>
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
//            getHtmlPicture(url+matcher.group(3));
            pics.add(matcher.group(3));
            System.out.println("找到资源路径:"+matcher.group(3));
        }

//        pattern = Pattern.compile(searchImgReg2);
//        matcher = pattern.matcher(content);
//        while (matcher.find()) {
//            System.out.println(matcher.group(3));
//            getHtmlPicture(matcher.group(3));
//        }
// searchImgReg =
// "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
    }
    public static String getHtmlCode(String httpUrl) throws IOException {
        String content ="";
        URL uu = new URL(httpUrl); // 创建URL类对象
        BufferedReader ii = new BufferedReader(new InputStreamReader(uu
                .openStream())); // //使用openStream得到一输入流并由此构造一个BufferedReader对象
        String input;
        while ((input = ii.readLine()) != null) { // 建立读取循环，并判断是否有读取值
            content += input;
        }
        ii.close();
        return content;
    }
    /**
     * @Author GongXl
     * @Description  下载
     * @Date 004 10:55
     * @Param [httpUrl]
     * @return void
    **/
    public static void getHtmlPicture(String httpUrl) {
        System.out.println(""+httpUrl);
        URL url;
        BufferedInputStream in;
        FileOutputStream file;
        try {
            System.out.println("取网络图片");
            String fileName = httpUrl.substring(httpUrl.lastIndexOf("/"));
            String filePath = "C:/Pic/";
            url = new URL(httpUrl);

            in = new BufferedInputStream(url.openStream());

            file = new FileOutputStream(new File(filePath+fileName));
            int t;
            while ((t = in.read()) != -1) {
                file.write(t);
            }
            file.close();
            in.close();
            System.out.println("图片获取成功");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Author GongXl
     * @Description
     * @Date 004 10:08
     * @Param [htmlStr]
     * @return java.util.List<java.lang.String>
    **/
    public static List<String> getImgSrc(String htmlStr) {
        String img = "";
        Pattern p_image;
        Matcher m_image;

//		 String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        /*
    <div><a href="https://www.tsinghua.edu.cn/kyzt/" target="_blank"><img src="js/zwky.jpg" style="width: 100%;"></a></div>
     <img src="images/logo_1.svg">
        **/
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            // Matcher m =
            // Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                System.out.println(m.group(0)+"##########"+m.group(1));
                pics.add(m.group(1));
            }
        }
        return pics;
    }


    public static void test1(String[] args) throws IOException {
        String urlAddress = "http://106.52.178.132:8080/test/docs/java1.pdf";
        InputStream is = null;
        OutputStream os = null;
        URL url = null;
        try {
            url = new URL(urlAddress);
            is = url.openStream();
            os = new FileOutputStream("e:/tmp/java1.pdf");
            byte[] bys = new byte[1024];
            long time = 0;
            long size = 0;
            int len = 0;
            while((len = is.read(bys))!=-1){
                os.write(bys, 0, len);
                time ++ ;
                if(time % 1024 == 0){
                    System.out.println("下载了"+(++size)+"Mb");
                }
            }
        } catch (MalformedURLException e){

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("下载完成");

        URL netchinaren = new URL ("http://www.atwyl.com/index.shtml");
        URLConnection uconn = netchinaren.openConnection( );

    }


}
